package com.xiaobing.user.controller;


import com.xiaobing.common.annotation.RequiresPermissions;
import com.xiaobing.common.entity.system.SysDept;
import com.xiaobing.common.entity.system.SysRole;
import com.xiaobing.common.entity.system.SysUser;
import com.xiaobing.common.povo.LoginUser;
import com.xiaobing.common.res.Result;
import com.xiaobing.common.utils.SecurityUtils;
import com.xiaobing.common.utils.StringUtils;
import com.xiaobing.user.service.ISysDeptService;
import com.xiaobing.user.service.ISysPermissionMenuService;
import com.xiaobing.user.service.ISysRoleService;
import com.xiaobing.user.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * token 控制
 */
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private ISysPermissionMenuService permissionService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;


    /**
     * 获取用户信息 /user/getInfo
     *
     * @return 用户信息
     */

    @RequiresPermissions("system:menu:list")
    @GetMapping("/getInfo")
    public Result getInfo() {
//        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return Result.success(loginUser);
    }


    /**
     * 获取当前用户信息  /user/info/test
     */
    @RequiresPermissions(value = {"system:menu:list"})
    @GetMapping("/info/{userName}")
    public Result<LoginUser> info(@PathVariable("userName") String userName) {
        SysUser sysUser = userService.selectUserByUserName(userName);
        if (StringUtils.isNull(sysUser)) {
            return Result.error("未查询到用户信息！");
        }
        // 角色集合
        List<SysRole> sysRoles = roleService.selectRoleByUserId(sysUser.getId());
        Set<String> roles = new HashSet<>();
        for (SysRole sysRole : sysRoles) {
            if (StringUtils.isNotNull(sysRole)) {
                roles.addAll(Arrays.asList(sysRole.getRoleKey().trim().split(",")));
            }
        }
        Set<String> permissions = new HashSet<>();
        // 权限集合
        if (sysRoles != null && sysRoles.size() > 0) {
            sysUser.setRoles(sysRoles);
            permissions = permissionService.getMenuPermission(sysUser);
        }
        //  根据用户id查询部门
        List<SysDept>  deptList = deptService.selectDeptListByUserId(sysUser);
        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(sysUser);
        loginUser.setRoles(roles);
        loginUser.setPermissions(permissions);
        loginUser.setDeptList(deptList);
        return Result.success(loginUser);
    }


}
